<template>
    <div class="publicpageview">
        <div class="publitableview">
            <el-button type="primary" size="mini" @click="handleAddUser('add')">新增部门</el-button>
            <el-table :data="list" border fit highlight-current-row size="mini">
                <el-table-column align="center" label="部门编号" prop="did">
                </el-table-column>

                <el-table-column align="center" label="部门名称" prop="dname">
                </el-table-column>



                <el-table-column align="center" label="操作" width="120">
                    <template slot-scope="scope">
                        <el-button type="text" size="mini" @click="handleUpdate(scope.row, 'update')">修改</el-button>
                        <el-button type="text" size="mini" @click="handleUpdate(scope.row,'details')">详情</el-button>
                        <el-button type="text" size="mini" @click="handleDelete(scope.row.did)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="publicpaginationview">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="listQuery.pageNum" background :page-sizes="[10, 20, 50, 100]"
                    :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
                    small></el-pagination>
            </div>

        </div>

        <el-dialog :title="isAdd == 'add' ? '新增部门' : isAdd == 'update' ? '修改部门' : '部门详情'" :visible.sync="dialogVisible"
            width="40%" :before-close="handleClose">
            <el-form :model="formData" ref="dataForm" class="demo-ruleForm" label-position="right" label-width="120px"
                size="mini">
                <el-form-item label="部门编号：" prop="did">
                    <el-input v-model="formData.did" disabled></el-input>
                </el-form-item>
                <el-form-item label="部门名称：" prop="dname">
                    <el-input v-model="formData.dname"></el-input>
                </el-form-item>



            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleClose" size="medium" v-if="isAdd == 'details'">返
                    回</el-button>

                <el-button type="primary" @click="save" size="medium" v-else>保 存</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { districtlist, districtcreate, districtupdate, districtdelete } from '@/api/system'
export default {
    name: "AccommodationManagementIndex",

    data() {
        return {
            listQuery: {
                pageNum: 1,
                pageSize: 10,
                keyword: "",
                status: "",
                // phone: "",
            },
            total: 0,

            list: [

            ],

            dialogVisible: false,
            isAdd: 'add',
            formData: {
                did: "",
                dname: ""
            },
        };
    },

    computed: {
        deptName() {
            return (val) => {
                if (val == "1") {
                    return "事业部";
                }
                return "生活部";
            };
        },
    },

    mounted() {
        this.getList();
    },

    methods: {

        handleSizeChange(val) {
            this.listQuery.pageNum = 1;
            this.listQuery.pageSize = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.listQuery.pageNum = val;
            this.getList();
        },
        getList() {
            districtlist(this.listQuery).then(res => {
                this.list = res.data.list
                this.total = res.data.total
            })

        },

        resetForm(val) {

            this.$refs[val].resetFields();
        },
        save() {
            if (this.isAdd == 'add') {
                districtcreate({ ...this.formData }).then(res => {
                    this.handleClose();
                    this.$message.success("操作成功")
                    this.getList();
                })
            }

            if (this.isAdd == 'update') {
                districtupdate({ ...this.formData }).then(res => {
                    this.handleClose();
                    this.$message.success("操作成功")
                    this.getList();
                })
            }
        },

        handleAddUser(type) {
            this.isAdd = 'add';
            this.dialogVisible = true;
        },

        handleClose() {
            this.resetForm("dataForm");
            this.dialogVisible = false;
        },

        handleUpdate(currentData, type) {
            this.isAdd = type;
            //   一定要在打开弹窗前 再赋值 不然resetFiles没用
            this.dialogVisible = true;
            this.$nextTick(() => {
                this.formData = { ...currentData };
            });
        },

        handleDelete(id) {
            this.$confirm(
                "此操作将删除该条内容，请确认是否继续操作？",
                "删除提示",
                {
                    confirmButtonText: "保 存",
                    cancelButtonText: "取 消",
                    type: "warning",
                }
            )
                .then(() => {
                    districtdelete(id).then(res => {
                        this.getList();
                        this.$message.success("操作成功")
                    })
                })
                .catch(() => { });
        }
    },
};
</script>